package com.jvup.gameshow.controller;

import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;

import com.jvup.common.support.base.EventController;
import com.jvup.common.constant.Navigation;
import com.jvup.gameshow.BR;
import com.jvup.gameshow.databinding.FragmentPlayerShowBinding;
import com.jvup.gameshow.databinding.LayoutPlayerShowIdeaBinding;
import com.jvup.gameshow.fragment.PlayerShowFragment;
import com.jvup.gameshow.model.viewModel.impl.PlayerShowViewModel.PlayerShowIdeaViewModel;
import com.jvup.common.widget.adapter.auto.AutoVariable;
import com.jvup.gameshow.view.tablayout.TabLayout;
import com.qire.antsrouter.AntsRouter;
import com.qire.antsrouter.card.FloorCard;

import java.util.HashMap;

public class PlayerShowController extends EventController<PlayerShowFragment> implements AutoVariable<LayoutPlayerShowIdeaBinding> {

    public static final float MAX_SCALE = 1.3f;
    private HashMap<Integer, Float> mLastMap = new HashMap<>();

    public PlayerShowController(PlayerShowFragment fragment) {
        super(fragment);
    }

    @Override
    protected void bindView() {
        FragmentPlayerShowBinding viewDataBinder = owner.viewDataBinder();
        if(viewDataBinder != null) {
            viewDataBinder.setVariable(BR.playerShowController,this);
//            viewDataBinder.setPlayerShowController(this);
        }
    }

    @Override
    public void bindVariable(LayoutPlayerShowIdeaBinding viewBind) {
        viewBind.setPlayerShowController(this);
    }

    public void transformPage(@NonNull View view, float v) {
        if (v > -1 && v < 1) {
//            int currPosition = (int) view.getTag();
            FragmentPlayerShowBinding viewDataBinder = owner.viewDataBinder();
            int currPosition = viewDataBinder.playerShowZone.getCurrentItem();
            final float currV = Math.abs(v);
            if (!mLastMap.containsKey(currPosition)) {
                mLastMap.put(currPosition, currV);
                return;
            }
            float lastV = mLastMap.get(currPosition);
            // 获取当前 TabView 的 TextView
            LinearLayout ll = (LinearLayout) viewDataBinder.playerShowZoneTab.getChildAt(0);
            TabLayout.TabView tb = (TabLayout.TabView) ll.getChildAt(currPosition);
            View textView = tb.getTextView();

            // 先判断是要变大还是变小
            // 如果 currV > lastV，则为变小；如果 currV < lastV，则为变大
            if (currV > lastV) {
                float leavePercent = currV; // 计算离开屏幕的百分比
                // 变小
                textView.setScaleX(MAX_SCALE - (MAX_SCALE - 1.0f) * leavePercent);
                textView.setScaleY(MAX_SCALE - (MAX_SCALE - 1.0f) * leavePercent);
            } else if (currV < lastV) {
                float enterPercent = 1 - currV; // 进入屏幕的百分比
                // 变大
                textView.setScaleX(1.0f + (MAX_SCALE - 1.0f) * enterPercent);
                textView.setScaleY(1.0f + (MAX_SCALE - 1.0f) * enterPercent);
            }
            mLastMap.put(currPosition, currV);
        }
    }

    public void playerIdeaOnClick(View view) {
        PlayerShowIdeaViewModel ideaViewModel = (PlayerShowIdeaViewModel) view.getTag();
        AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.IdeaDetailPage)
                .withData("ideaId",ideaViewModel.getId())
                .navigation();
    }

    public void playerIdeaNicknameOnClick(View view) {
        PlayerShowIdeaViewModel ideaViewModel = (PlayerShowIdeaViewModel) view.getTag();
        AntsRouter.ROUTER.<FloorCard>buildCard(Navigation.Page.PlayerDynamicPage)
                .withData("userId",ideaViewModel.getUserId())
                .navigation();
    }

    public void playerIdeaGiveLikeOnClick(View view) {
        PlayerShowIdeaViewModel ideaViewModel = (PlayerShowIdeaViewModel) view.getTag();
        ideaViewModel.toggleGiveLike();
    }

}
